package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.ScenicDao;
import com.aaa.ssm.dao.TSbsnActDao;
import com.aaa.ssm.dao.TSbsnCode;
import com.aaa.ssm.dao.TSbsnOrderDao;
import com.aaa.ssm.entity.Scenic;
import com.aaa.ssm.entity.TSbsnAct;
import com.aaa.ssm.entity.TSbsnOrder;
import com.aaa.ssm.service.TSbsnOrderService;
import com.aaa.ssm.util.ConstantUtil;
import com.aaa.ssm.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TSbsnOrderServiceImpl implements TSbsnOrderService {
    @Resource
    private TSbsnOrderDao tsbsnOrderDao;
    @Resource
    private ScenicDao scenicDao;
    @Resource
    private TSbsnActDao tSbsnActDao;


    /**
     * 封装格式化时间类
     * @return
     */
    private static String time(){
        //获取当前时间
        Date date = new Date();
        //实例化格式化时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //format格式化时间
        String format = simpleDateFormat.format(date);
        return format;
    }


    /**
     * 分页带参查询
     * @param tSbsnOrder
     * @return
     */
    @Override
    public PageInfo queryPage(Page<TSbsnOrder> tSbsnOrder) {
        PageHelper.startPage(tSbsnOrder.getPageNo(),tSbsnOrder.getPageSize());
        return new PageInfo(tsbsnOrderDao.queryPage(tSbsnOrder.getData()));
    }

    /**
     * 根据用户编号查询订单数据
     * @param tSbsnOrder
     * @return
     */
    @Override
    public PageInfo queryMyOrder(Page<TSbsnOrder> tSbsnOrder) {
        PageHelper.startPage(tSbsnOrder.getPageNo(),tSbsnOrder.getPageSize());
        return new PageInfo(tsbsnOrderDao.queryMyOrder(tSbsnOrder.getData().getUserid()));
    }

    @Override
    public TSbsnOrder queryById(Integer orderid) {
        List<TSbsnOrder> tSbsnOrders = tsbsnOrderDao.queryById(orderid);
        if (tSbsnOrders!=null&&tSbsnOrders.size()>0){
            return tSbsnOrders.get(0);
        }
        return null;
    }

    @Override
    public TSbsnOrder TSbsnOrderAdd(TSbsnOrder tSbsnOrder) {
        //设置参数 代付款
        tSbsnOrder.setCodeid(ConstantUtil.OrderStatus.PENDING_PAYMENT);
        tSbsnOrder.setState(ConstantUtil.OrderStatus.PENDING_PAYMENT);
        tSbsnOrder.setOrdertime(time());
        //返回添加的主键
        int add = tsbsnOrderDao.TSbsnOrderAdd(tSbsnOrder);
        //添加状态
        TSbsnAct tSbsnAct = new TSbsnAct();
        tSbsnAct.setOrderid(tSbsnOrder.getOrderid());
        tSbsnAct.setCodeid(tSbsnOrder.getCodeid());
        System.out.println(tSbsnOrder.getCodeid());
        tSbsnAct.setActcode(tSbsnOrder.getCodeid());
        System.out.println(tSbsnAct.getActcode());
        tSbsnAct.setBz("下单");
        tSbsnAct.setCztime(tSbsnOrder.getOrdertime());
        tSbsnAct.setCzrid(tSbsnOrder.getUserid());
        //调用添加
        tSbsnActDao.add(tSbsnAct);

        //景区添加票数
        Scenic scenic = scenicDao.queryBy(tSbsnOrder.getScenicid());
        Integer onums = scenic.getOnums();
        scenic.setOnums(onums+1);
        //修改订票信息
        scenicDao.update(scenic);

        return tSbsnOrder;
    }

    @Override
    public int TSbsnOrderUpdate(TSbsnOrder tSbsnOrder) {
        tSbsnOrder.setCodeid(ConstantUtil.OrderStatus.PAID);
        tSbsnOrder.setState(ConstantUtil.OrderStatus.PAID);
        int update = tsbsnOrderDao.TSbsnOrderUpdate(tSbsnOrder);
        //设置订单状态
        TSbsnAct tSbsnAct = tSbsnActDao.queryById(tSbsnOrder.getOrderid());
        tSbsnAct.setCodeid(tSbsnOrder.getCodeid());
        tSbsnAct.setActcode(tSbsnOrder.getCodeid());
        tSbsnAct.setBz("付款成功");
        tSbsnActDao.update(tSbsnAct);
        //更新景区门票
        Scenic scenic = scenicDao.queryBy(tSbsnOrder.getScenicid());
        Integer snums = scenic.getSnums();
        scenic.setSnums(snums+1);
        Integer onums = scenic.getOnums();
        scenic.setOnums(onums-1);
        scenicDao.update(scenic);
        return update;
    }

    @Override
    public int TSbsnOrderDelete(String orderno) {
        return tsbsnOrderDao.TSbsnOrderDelete(orderno);
    }
}
